package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC2024 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int maxConsecutiveAnswers(String s, int k) {
            return Math.max(getMaxConsecutiveAnswersForGivenChar(s, k, 'T'), getMaxConsecutiveAnswersForGivenChar(s, k, 'F'));
        }

        public int getMaxConsecutiveAnswersForGivenChar(String s, int k, char givenCh) {
            int left = 0, right = 0, unMatchCnt = 0, ans = 0;
            while (right < s.length()) {
                if (s.charAt(right) != givenCh) {
                    unMatchCnt++;
                    while (unMatchCnt > k && left <= right) {
                        if (s.charAt(left) != givenCh) {
                            unMatchCnt--;
                        }
                        left++;
                    }
                }

                ans = Math.max(ans, right - left + 1);
                right++;
            }

            return Math.max(ans, right - left);
        }
    }

    public void run() {
        System.out.println(new Solution().maxConsecutiveAnswers("TTFF", 2));
        System.out.println(new Solution().maxConsecutiveAnswers("TFFT", 1));
        System.out.println(new Solution().maxConsecutiveAnswers("TTFTTFTT", 1));
    }

    public static void main(String[] args) throws Exception {
        LC2024 an = new LC2024();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
